package com.example.mystore.mapper;

import com.example.mystore.model.Order;
import org.apache.ibatis.annotations.*;


import java.math.BigInteger;
import java.util.List;

@Mapper
public interface OrderMapper {

    @Insert("INSERT INTO `order` (order_no, uid, addr_id, amount, type, freight, status, payment_time, delivery_time, finish_time, close_time, created, updated) " +
            "VALUES (#{orderNo}, #{uid}, #{addrId}, #{amount}, #{type}, #{freight}, #{status}, #{paymentTime}, #{deliveryTime}, #{finishTime}, #{closeTime}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertOrder(Order order);

    @Delete("DELETE FROM `order` WHERE id = #{id}")
    int deleteOrderById(int id);

    @Update("UPDATE `order` SET order_no = #{orderNo}, uid = #{uid}, addr_id = #{addrId}, amount = #{amount}, type = #{type}, freight = #{freight}, status = #{status}, " +
            "payment_time = #{paymentTime}, delivery_time = #{deliveryTime}, finish_time = #{finishTime}, close_time = #{closeTime}, created = #{created}, updated = #{updated} WHERE id = #{id}")
    int updateOrder(Order order);

    @Select("SELECT * FROM `order` WHERE uid = #{userId}")
    List<Order> selectOrderById(Integer userId);

    @Select("SELECT * FROM `order`")
    @Results(id = "orderResultMap", value = {
            @Result(property = "orderNo", column = "order_no")
    })
    List<Order> selectAllOrders();

    @Select("SELECT * FROM `order` WHERE order_no = #{orderNo}")
    @ResultMap("orderResultMap")
    Order getOrderById(@Param("orderNo") Integer orderNo);

    @Select("SELECT COUNT(*) FROM `order` WHERE order_no = #{orderNo}")
    boolean existsOrderByOrderNo(@Param("orderNo") BigInteger orderNo);

    @Select("SELECT * FROM `order` WHERE order_no = #{orderNo}")
    Order selectOrderByNo(int orderNo);


}
